package com.charley.icloud.admin.webapps.sysdept.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.charley.icloud.admin.webapps.sysdept.dao.SysDeptMapper;
import com.charley.icloud.admin.webapps.sysdept.pojo.entity.SysDeptDO;
import com.charley.icloud.admin.webapps.sysdept.pojo.model.SysDeptVO;
import com.charley.icloud.admin.webapps.sysdept.service.SysDeptService;
import com.charley.icloud.common.constants.SystemNumberConstants;
import com.charley.icloud.datasource.page.MybatisPageHelper;
import com.charley.icloud.datasource.page.PageRequest;
import com.charley.icloud.datasource.page.PageResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SysDeptServiceImpl implements SysDeptService {

    @Resource
    private SysDeptMapper sysDeptMapper;

    @Override
    public int save(SysDeptDO record) {
        if (record.getId() == null || record.getId() == 0) {
            int orderNum = sysDeptMapper.findOrderNumByParentId(record.getParentId());
            record.setOrderNum(orderNum);
            record.setCreateBy("System");
            record.setCreateTime(new Date());
            // 新增
            return sysDeptMapper.insertSelective(record);
        }
        record.setLastUpdateBy("System");
        record.setLastUpdateTime(new Date());
        // 修改
        return sysDeptMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int delete(SysDeptDO record) {
        return sysDeptMapper.deleteByPrimaryKey(record);
    }

    @Override
    public int delete(List<SysDeptDO> records) {
        Date nowDate = new Date();
        for (SysDeptDO record : records) {
            record.setLastUpdateBy("System");
            record.setLastUpdateTime(nowDate);
            delete(record);
        }
        return 1;
    }

    @Override
    public SysDeptDO findById(Long id) {
        return sysDeptMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageResult findPage(PageRequest pageRequest) {
        return MybatisPageHelper.findPage(pageRequest, sysDeptMapper);
    }

    @Override
    public List<SysDeptVO> findTree() {
        List<SysDeptVO> sysDeptVOs = new ArrayList<>();
        List<SysDeptDO> depts = sysDeptMapper.findAll();
        for (SysDeptDO dept : depts) {
            if (dept.getParentId() == null || SystemNumberConstants.SYSTEM_NUMBER_LONG_ZERO.equals(dept.getParentId())) {
                SysDeptVO sysDeptVO = new SysDeptVO();
                BeanUtil.copyProperties(dept,sysDeptVO, CopyOptions.create().ignoreCase().ignoreNullValue());
                sysDeptVO.setLevel(0);
                sysDeptVOs.add(sysDeptVO);
            }
        }
        findChildren(sysDeptVOs, depts);
        return sysDeptVOs;
    }

    private void findChildren(List<SysDeptVO> sysDeptVOs, List<SysDeptDO> depts) {
        for (SysDeptVO sysDept : sysDeptVOs) {
            List<SysDeptVO> children = new ArrayList<>();
            for (SysDeptDO dept : depts) {
                if (dept.getId() != null && sysDept.getId().equals(dept.getParentId())) {
                    SysDeptVO sysDeptVO = new SysDeptVO();
                    BeanUtil.copyProperties(dept,sysDeptVO, CopyOptions.create().ignoreCase().ignoreNullValue());
                    sysDeptVO.setParentName(sysDept.getName());
                    sysDeptVO.setLevel(sysDept.getLevel() + 1);
                    children.add(sysDeptVO);
                }
            }
            sysDept.setChildren(children);
            // 递归调用
            findChildren(children, depts);
        }
    }
}
